package com.wfuhui.modules.api.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wfuhui.common.utils.R;
import com.wfuhui.modules.api.annotation.LoginUser;
import com.wfuhui.modules.oms.entity.UserEntity;
import com.wfuhui.modules.oms.entity.UserAddressEntity;
import com.wfuhui.modules.oms.service.UserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/userAddress/")
@Api("用户地址接口")
public class ApiUserAddressController {
	
	@Autowired
	private UserAddressService userAddressService;
	
    @GetMapping("dft")
    @ApiOperation(value = "获取用户默认地址")
    public R dft(@LoginUser UserEntity user){
    	UserAddressEntity userAddress = userAddressService.queryByDefault(user.getId());
        return R.ok().put("userAddress", userAddress);
    }
    
    @GetMapping("setDft")
    public R setDft(@LoginUser UserEntity user, Integer id){
		UserAddressEntity userAddress = new UserAddressEntity();
		userAddress.setId(id);
		userAddress.setDft(true);
		userAddress.setUserId(user.getId());
		userAddressService.updateDft(userAddress);
        return R.ok();
    }
    
    @GetMapping("detail")
    @ApiOperation(value = "获取地址详情")
    public R detail(Integer id){
    	UserAddressEntity userAddress = userAddressService.queryObject(id);
        return R.ok().put("userAddress", userAddress);
    }
    
    @GetMapping("add")
    @ApiOperation(value = "添加地址")
    public R add(@LoginUser UserEntity user, UserAddressEntity userAddress) {
    	userAddress.setUserId(user.getId());
    	userAddress.setUpdateTime(new Date());
    	userAddress.setDft(true);
    	userAddressService.save(userAddress);
    	return R.ok();
    }
    
    @GetMapping("update")
    @ApiOperation(value = "修改地址")
    public R update(@LoginUser UserEntity user, UserAddressEntity userAddress) {
    	userAddressService.update(userAddress);
    	return R.ok();
    }
    
    @GetMapping("delete")
    @ApiOperation(value = "删除地址")
    public R delete(Integer id) {
    	userAddressService.delete(id);
    	return R.ok();
    }
    
    @GetMapping("list")
    @ApiOperation(value = "获取用户所有地址")
    public R list(@LoginUser UserEntity user){
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("userId", user.getId());
    	List<UserAddressEntity> userAddressList = userAddressService.queryList(map);
        return R.ok().put("userAddressList", userAddressList);
    }
}
